<template>
	<view class="page">
		<!-- 背景 -->
		<view class="head-bg">
			<view class="vip-title">
				<text>开通超级会员</text>
			</view>
			<view class="vip-describe">
				<text>专享9大权益</text>
			</view>
		</view>
		<!-- 会员权益 -->
		<view class="vip-interests">
			<view class="interests-list">
				<view class="list" v-for="(item, index) in rulesInfo" :key="index">
					<view class="thumb">
						<image :src="item.imageUrl" mode=""></image>
					</view>
					<view class="title">{{ item.title }}</view>
					<view class="describe">{{ item.introduce }}</view>
				</view>
			</view>
			<view class="vip-tips">
				<text class="iconfont icon-qian"></text>
				<text>开通超级会员预计一年可省</text>
				<text class="price">999</text>
				<text>元</text>
			</view>
			<view class="dredge-btn">
				<text>立即开通￥199/年</text>
			</view>
			<view class="exchange-btn">
				<text @click="getUserStatusCouponMultipleAdd()">兑换会员</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				rulesInfo: [], // 用来存储接口返回的规则数据
				loading: false
			};
		},
		onLoad() {
			this.getCouponByPoints()
		},
		methods: {
			getCouponByPoints() {
				uni.request({
					url: "http://127.0.0.1:8899/coupon/member/list",
					method: "POST",
					data: {},
					success: (res) => {
						this.rulesInfo = res.data.data.rules_info
					},
					fail: (err) => {
						console.error('接口请求失败:', err);
					}
				})
			},
			// 兑换会员方法
			getUserStatusCouponMultipleAdd() {
				if (this.loading || this.hasRedeemed) return; // 如果已经兑换过或者正在加载，则不进行处理
				this.loading = true;
				this.hasRedeemed = true; // 标记已兑换，禁用按钮
				uni.request({
					url: "http://127.0.0.1:8899/coupon/multiple/add/user/vip",
					method: "POST",
					header: {
						'Content-Type': 'application/json' // 设置请求头
					},
					data: {
						user_id: 8, // 固定用户ID
						user_status: 6 // 固定状态值
					},
					success: (res) => {
						if (res.data.code === 200) {
							uni.showModal({
								title: '提示',
								content: '兑换成功',
								showCancel: false
							})
						} else {
							uni.showModal({
								title: '提示',
								content: res.data.message || '用户已兑换',
								showCancel: false
							})
						}
					},
					complete: () => {
						uni.showModal({
							title: '提示',
							content: res.data.message || '用户已兑换',
							showCancel: false
						})
					}
				});
			},
		}
	}
</script>

<style scoped lang="scss">
	@import 'MembersOpened.scss';
</style>